﻿using System;
using System.Collections.Generic;
using ProjScan.Tools;
using UnityEngine;

namespace ProjScan.Submodules.Art_Assets_Check
{
	// Token: 0x0200019E RID: 414
	public static class ArtCheckCommonMethods
	{
		// Token: 0x06000DF7 RID: 3575 RVA: 0x0003F2F8 File Offset: 0x0003D4F8
		public static void CheckUVDensity(GameObject prefab, ref float averageDensity, ref float varianceOfDensity)
		{
			Renderer[] componentsInChildren = prefab.GetComponentsInChildren<Renderer>();
			bool flag = componentsInChildren == null || componentsInChildren.Length == 0;
			if (flag)
			{
				throw new Exception("CheckUVDensityFailed: Array renderers is null or empty");
			}
			List<float> list = new List<float>();
			foreach (Renderer renderer in componentsInChildren)
			{
				Mesh mesh = null;
				float textureArea = 0f;
				bool flag2;
				try
				{
					flag2 = ArtCheckCommonMethods.CollectMeshAndTextureDimensions(renderer, out mesh, out textureArea);
				}
				catch (Exception exception_)
				{
					flag2 = false;
					ScanLog.Instance.LogException(exception_, "ArtCheckCommonMethods.CheckUvDensity.CollectFailed", prefab.name + " - " + mesh.name);
				}
				bool flag3 = flag2;
				if (flag3)
				{
					try
					{
						ArtCheckCommonMethods.CalculateFaceDensities(mesh, textureArea, ref list);
					}
					catch (Exception ex)
					{
						ScanLog.Instance.LogInfo(ScanLog.LogLevel.INFO, "ArtCheckCommonMethods.GetPerFaceDensity Exception", string.Format("{0}\n{1}\n{2}", ex.GetType(), ex.Message, ex.StackTrace));
					}
				}
			}
			bool flag4 = list == null || list.Count == 0;
			if (flag4)
			{
				throw new Exception("CheckUVDensityFailed: List perFaceDensity is null or empty");
			}
			try
			{
				ShaderUtility.CalculateAverageAndVariance(list, out averageDensity, out varianceOfDensity);
			}
			catch (Exception ex2)
			{
				ScanLog.Instance.LogInfo(ScanLog.LogLevel.INFO, "ArtCheckCommonMethods.GetPerFaceDensity Exception", string.Format("{0}\n{1}\n{2}", ex2.GetType(), ex2.Message, ex2.StackTrace));
				throw;
			}
		}

		// Token: 0x06000DF8 RID: 3576 RVA: 0x0003F478 File Offset: 0x0003D678
		private static bool CollectMeshAndTextureDimensions(Renderer renderer, out Mesh mesh, out float dimension)
		{
			Texture texture = null;
			bool flag = renderer.sharedMaterial != null;
			if (flag)
			{
				texture = renderer.sharedMaterial.mainTexture;
			}
			else
			{
				ScanLog.Instance.LogInfo(ScanLog.LogLevel.INFO, "ArtCheckMethod.CollectMeshAndTex", "sharedMaterial is null");
			}
			mesh = null;
			bool flag2 = texture == null;
			bool result;
			if (flag2)
			{
				ScanLog.Instance.LogInfo(ScanLog.LogLevel.INFO, "ArtCheckMethod.CollectMeshAndTex", "mainTex is null");
				dimension = 0f;
				result = false;
			}
			else
			{
				dimension = (float)(texture.width * texture.height) * renderer.sharedMaterial.mainTextureScale.x * renderer.sharedMaterial.mainTextureScale.y;
				if (!(renderer is MeshRenderer))
				{
					SkinnedMeshRenderer skinnedMeshRenderer = renderer as SkinnedMeshRenderer;
					if (skinnedMeshRenderer != null)
					{
						ScanLog.Instance.LogInfo(ScanLog.LogLevel.INFO, "ArtCheckMethod.CollectMeshAndTex", "renderer is SkinnedMeshRenderer");
						mesh = skinnedMeshRenderer.sharedMesh;
					}
				}
				else
				{
					ScanLog.Instance.LogInfo(ScanLog.LogLevel.INFO, "ArtCheckMethod.CollectMeshAndTex", "renderer is MeshRenderer");
					MeshFilter component = renderer.gameObject.GetComponent<MeshFilter>();
					mesh = component.sharedMesh;
				}
				result = true;
			}
			return result;
		}

		// Token: 0x06000DF9 RID: 3577 RVA: 0x0003F598 File Offset: 0x0003D798
		private static void CalculateFaceDensities(Mesh mesh, float textureArea, ref List<float> densities)
		{
			bool flag = mesh == null;
			if (flag)
			{
				throw new Exception("Mesh is null");
			}
			bool flag2 = densities == null;
			if (flag2)
			{
				throw new Exception("perFaceDensity is null");
			}
			bool flag3 = mesh.triangles == null;
			if (flag3)
			{
				throw new Exception("mesh.triangles is null");
			}
			bool flag4 = mesh.triangles.Length == 0;
			if (flag4)
			{
				throw new Exception("mesh.triangles is empty");
			}
			for (int i = 0; i < mesh.triangles.Length; i += 3)
			{
				Vector3 vector = mesh.vertices[mesh.triangles[i]];
				Vector3 vector2 = mesh.vertices[mesh.triangles[i + 1]];
				Vector3 vector3 = mesh.vertices[mesh.triangles[i + 2]];
				float magnitude = Vector3.Cross(vector - vector2, vector - vector3).magnitude;
				Vector3 vector4 = Vector3.Scale(mesh.uv[mesh.triangles[i]], Vector3.one);
				Vector3 vector5 = Vector3.Scale(mesh.uv[mesh.triangles[i + 1]], Vector3.one);
				Vector3 vector6 = Vector3.Scale(mesh.uv[mesh.triangles[i + 2]], Vector3.one);
				float magnitude2 = Vector3.Cross(vector4 - vector5, vector4 - vector6).magnitude;
				float num = textureArea * magnitude2 / magnitude;
				densities.Add(Mathf.Sqrt(num));
			}
		}
	}
}
